﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;


namespace LiveProjectManager.DDD.Services
{
    public class TaskService : Base.Service<Domains.Task>
    {
        private DDD.Services.UserService _userService;


        public TaskService() : base()
        {
            _userService = new DDD.Services.UserService();
        }


        public bool update(
            long myId,
            string myTitle,
            Domains.User myUser,
            int myProgression,
            Domains.TaskPriority myPriority,
            string myDescription = null,
            bool autoSave = true
        ) 
        { 
            // Try to retrieve requested task.
            Domains.Task myUpdatedTask = getById(myId);
            if (myUpdatedTask == null)
                return false;

            if (myTitle != myUpdatedTask.Title)
                myUpdatedTask.Title = myTitle;

            if (myUser != myUpdatedTask.User)
                myUpdatedTask.User = myUser;

            if (myProgression != myUpdatedTask.Progression)
                myUpdatedTask.Progression = myProgression;

            if (myPriority != myUpdatedTask.Priority)
                myUpdatedTask.Priority = myPriority;

            if (myDescription != null && (myDescription.Length != myUpdatedTask.Description.Length || myDescription != myUpdatedTask.Description))
                myUpdatedTask.Description = myDescription;

            if (autoSave)
                _context.SaveChanges();
            return true;
        }


        override public ICollection<Domains.Task> getAll(int paginateBy = 0, int page = 1)
        {
            if (paginateBy > 0)
            {
                if (page <= 0)
                    page = 1;
                return (from t in _repository orderby t.Id select t).Skip(paginateBy * (page - 1)).Take(paginateBy).ToList();
            }
            return (from t in _repository orderby t.Id select t).ToList();
        }


        override public Domains.Task getById(long myId)
        {
            ICollection<Domains.Task> myResults = (
                from t in _repository
                where t.Id == myId
                select t
            ).ToList();
            return (myResults.Count != 1) ? null : myResults.First();
        }


        public ICollection<Domains.Task> getAllByAssignee(Domains.User myAssignee, int paginateBy = 0, int page = 1)
		{
            if (paginateBy > 0)
            {
                if (page <= 0)
                    page = 1;
                return (from t in _repository where t.User.Id == myAssignee.Id orderby t.Id select t).Skip(paginateBy * (page - 1)).Take(paginateBy).ToList();
            }
            return (from t in _repository where t.User.Id == myAssignee.Id orderby t.Id select t).ToList();
        }


        public ICollection<Domains.Task> getAllOpen()
		{
			return (from t in _repository
                    where t.Progression != Domains.Task.MAXIMAL_PROGRESSION 
                    select t).ToList();
        }


        public ICollection<Domains.Task> getAllOpenByAssignee(Domains.User myAssignee)
		{
			return (from t in _repository
                    where t.User.Id == myAssignee.Id && t.Progression != Domains.Task.MAXIMAL_PROGRESSION 
                    select t).ToList();
        }


        public ICollection<Domains.Task> getAllClosed()
		{
			return (from t in _repository
                    where t.Progression == Domains.Task.MAXIMAL_PROGRESSION 
                    select t).ToList();
        }


        public ICollection<Domains.Task> getAllClosedByAssignee(Domains.User myAssignee)
		{
			return (from t in _repository
                    where t.User.Id == myAssignee.Id && t.Progression == Domains.Task.MAXIMAL_PROGRESSION 
                    select t).ToList();
        }


        public bool create(
            string myTitle, 
            Domains.User myUser, 
            int myProgression,
            Domains.TaskPriority myPriority = Domains.Task.DEFAULT_PRIORITY, 
            string myDescription = null, 
            bool autoSave = true
        )
		{
            Domains.Task myNewTask = new Domains.Task { 
                Title = myTitle,
                User = myUser,
                Priority = myPriority,
                Progression = myProgression,
                CreatedOn = DateTime.Now
            };
            if (myDescription != null && myDescription.Length != 0)
                myNewTask.Description = myDescription;
            _repository.Add(myNewTask);
            if (autoSave)
                _context.SaveChanges();
            return true;
        }


        override public bool remove(long myTaskId, bool autoSave = true)
        {
            Domains.Task myTask = getById(myTaskId);
            if (myTask == null)
                return false;
            _repository.Remove(myTask);
            if (autoSave)
                _context.SaveChanges();
            return true;
        }
    }
}